#!/bin/bash
#
# Author: Shuang.Ca <ylqjgm@gmail.com>
# Home: http://llnmp.com
# Blog: http://shuang.ca
#
# Version: 0.4 2014-02-07

function install() {
    cat /etc/issue
    uname -a
    MemTotal=`free -m | grep Mem | awk '{print $2}'`
    echo "\nMemory is: ${MemTotal} MB"

    #set timezone
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    apt-get install -y ntpdate
    ntpdate -u pool.ntp.org
    date

    #remove
    apt-get remove -y apache2 apache2-doc apache2-utils apache2.2-common apache2.2-bin apache2-mpm-prefork apache2-doc apache2-mpm-worker mysql-client mysql-server mysql-common php5 php5-common php5-cgi php5-mysql php5-curl php5-gd
    killall apache2
    dpkg -l |grep mysql 
    dpkg -P libmysqlclient15off libmysqlclient15-dev mysql-common 
    dpkg -l |grep apache 
    dpkg -P apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-common
    dpkg -l |grep php 
    dpkg -P php5 php5-common php5-cgi php5-mysql php5-curl php5-gd
    apt-get purge `dpkg -l | grep php| awk '{print $2}'`


    apt-get -y update
    apt-get -y upgrade

    apt-get -y install gcc g++ make cmake patch zip unzip wget libjpeg8 libjpeg8-dev libpng12-0 libpng12-dev libpng3 libfreetype6 libfreetype6-dev libxml2 libxml2-dev zlib1g zlib1g-dev libc6 libc6-dev libglib2.0-0 libglib2.0-dev bzip2 libzip-dev libmcrypt-dev libbz2-1.0 libncurses5 libncurses5-dev curl libcurl3 libcurl4-openssl-dev e2fsprogs libkrb5-3 libkrb5-dev libltdl-dev libidn11 libidn11-dev openssl libtool libevent-dev bison libsasl2-dev libxslt1-dev locales libcloog-ppl0

    bit=$(getconf LONG_BIT)
    [ "$bit" == "64" ] && apt-get -y install glibc.i686

    #check conf dir
    if [ ! -f $PWD_DIR/conf ]; then
        wget -c $GET_URI/llnmp/conf.tar.gz -O $PWD_DIR/conf.tar.gz

        cd $PWD_DIR
        tar zxvf conf.tar.gz
    fi

    #install jemalloc
    if [ "$jemalloc_install" == "y" ]; then
        [ ! -s $SRC_DIR/jemalloc-3.5.1.tar.bz2 ] && wget -c $GET_URI/jemalloc/jemalloc-3.5.1.tar.bz2 -O $SRC_DIR/jemalloc-3.5.1.tar.bz2

        cd $SRC_DIR
        tar xjvf jemalloc-3.5.1.tar.bz2
        cd jemalloc-3.5.1
        ./configure && make -j $cpu_num && make install

        echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
        ldconfig
    fi

    #install database
    useradd -M -s /sbin/nologin mysql
    rm -f /etc/my.cnf
    mkdir -p /data/mysql

    if [ "$db_select" == 2 ]; then
        [ ! -s $SRC_DIR/mariadb-5.5.36.tar.gz ] && wget -c $GET_URI/mariadb/mariadb-5.5.36.tar.gz -O $SRC_DIR/mariadb-5.5.36.tar.gz

        cd $SRC_DIR
        tar zxvf mariadb-5.5.36.tar.gz
        cd mariadb-5.5.36
    else
        [ ! -s $SRC_DIR/mysql-5.5.36.tar.gz ] && wget -c $GET_URI/mysql/mysql-5.5.36.tar.gz -O $SRC_DIR/mysql-5.5.36.tar.gz

        cd $SRC_DIR
        tar zxvf mysql-5.5.36.tar.gz
        cd mysql-5.5.36
    fi

    if [ "$jemalloc_install" == "y" ]; then
        COMMAND="-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc'"
    fi

    if [ "$db_select" == 2 ]; then
        cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_ARIA_STORAGE_ENGINE=1 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATEDX_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=OFF $COMMAND
    else
        cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=OFF $COMMAND
    fi
    make -j $cpu_num && make install

    cp support-files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    update-rc.d mysqld defaults

    cat >/etc/my.cnf<<EOF
[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
port = 3306
socket = /tmp/mysql.sock

basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1

skip-name-resolve
#skip-networking
back_log = 600

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30

log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log

performance_schema = 0

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF

    #optimize mysql
    Memtatol=`free -m | grep 'Mem:' | awk '{print $2}'`
    if [ $Memtatol -gt 1500 -a $Memtatol -le 2500 ]; then
    sed -i 's/table_open_cache = 128/table_open_cache = 256/g' /etc/my.cnf
    sed -i 's/tmp_table_size = 16M/tmp_table_size = 32M/g' /etc/my.cnf
    sed -i 's/thread_cache_size = 8/thread_cache_size = 16/g' /etc/my.cnf
    sed -i 's/query_cache_size = 8M/query_cache_size = 16M/g' /etc/my.cnf
    sed -i 's/innodb_buffer_pool_size = 64M/innodb_buffer_pool_size = 128M/g' /etc/my.cnf
    sed -i 's/myisam_sort_buffer_size = 8M/myisam_sort_buffer_size = 16M/g' /etc/my.cnf
    sed -i 's/key_buffer_size = 8M/key_buffer_size = 16M/g' /etc/my.cnf
    elif [ $Memtatol -gt 2500 -a $Memtatol -le 3500 ]; then
    sed -i 's/table_open_cache = 128/table_open_cache = 512/g' /etc/my.cnf
    sed -i 's/tmp_table_size = 16M/tmp_table_size = 64M/g' /etc/my.cnf
    sed -i 's/thread_cache_size = 8/thread_cache_size = 32/g' /etc/my.cnf
    sed -i 's/query_cache_size = 8M/query_cache_size = 32M/g' /etc/my.cnf
    sed -i 's/innodb_buffer_pool_size = 64M/innodb_buffer_pool_size = 512M/g' /etc/my.cnf
    sed -i 's/myisam_sort_buffer_size = 8M/myisam_sort_buffer_size = 32M/g' /etc/my.cnf
    sed -i 's/key_buffer_size = 8M/key_buffer_size = 64M/g' /etc/my.cnf
    elif [ $Memtatol -gt 3500 ];then
    sed -i 's/table_open_cache = 128/table_open_cache = 1024/g' /etc/my.cnf
    sed -i 's/tmp_table_size = 16M/tmp_table_size = 128M/g' /etc/my.cnf
    sed -i 's/thread_cache_size = 8/thread_cache_size = 64/g' /etc/my.cnf
    sed -i 's/query_cache_size = 8M/query_cache_size = 64M/g' /etc/my.cnf
    sed -i 's/innodb_buffer_pool_size = 64M/innodb_buffer_pool_size = 1024M/g' /etc/my.cnf
    sed -i 's/myisam_sort_buffer_size = 8M/myisam_sort_buffer_size = 64M/g' /etc/my.cnf
    sed -i 's/key_buffer_size = 8M/key_buffer_size = 256M/g' /etc/my.cnf
    fi

    /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

    chown mysql.mysql -R /data/mysql
    chgrp -R mysql /usr/local/mysql/.

    ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
    ln -s /usr/local/mysql/include/mysql /usr/include/mysql
    ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
    ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
    ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
    ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe

    service mysqld start

    /usr/local/mysql/bin/mysqladmin -u root password $dbpass

    cat >>/tmp/mysql_sec_script<<EOF
use mysql;
update user set password=password('$dbpass') where user='root';
delete from user where not (user='root') ;
delete from user where user='root' and password=''; 
drop database test;
DROP USER ''@'%';
flush privileges;
EOF

    /usr/local/mysql/bin/mysql -u root -p$dbpass -h localhost < /tmp/mysql_sec_script
    rm -f /tmp/mysql_sec_script
    ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib

    service mysqld restart

    #install litespeed
    useradd -M -s /sbin/nologin www
    mkdir -p /home/wwwroot/default

    if [ "$web_select" == 2 ]; then
        [ ! -s $SRC_DIR/openlitespeed-1.2.9.tgz ] && wget -c $GET_URI/openlitespeed/openlitespeed-1.2.9.tgz -O $SRC_DIR/openlitespeed-1.2.9.tgz

        cd $SRC_DIR
        tar zxvf openlitespeed-1.2.9.tgz
        cd openlitespeed-1.2.9
        ./configure --prefix=/usr/local/lsws --with-user=www --with-group=www --with-admin=$webuser --with-password=$webpass --with-email=$webemail --enable-adminssl=no --enable-spdy
        make -j $cpu_num && make install

        sed -i 's/<vhRoot>\$SERVER_ROOT\/DEFAULT\/<\/vhRoot>/<vhRoot>\/home\/wwwroot\/default\/<\/vhRoot>/g' /usr/local/lsws/conf/httpd_config.xml
        sed -i 's/<configFile>\$VH_ROOT\/conf\/vhconf\.xml<\/configFile>/<configFile>\$SERVER_ROOT\/conf\/default\.xml<\/configFile>/g' /usr/local/lsws/conf/httpd_config.xml
        sed -i "s/<address>\*:8088<\/address>/<address>\*:$port<\/address>/g" /usr/local/lsws/conf/httpd_config.xml

        cp $PWD_DIR/conf/vhconf.xml /usr/local/lsws/conf/default.xml
        rm -rf /usr/local/lsws/DEFAULT/
        mkdir -p /home/wwwlogs/litespeed

        service lsws restart
    else
        [ ! -s $SRC_DIR/lsws-4.2.7-std-i386-linux.tar.gz ] && wget -c $GET_URI/litespeed/lsws-4.2.7-std-i386-linux.tar.gz -O $SRC_DIR/lsws-4.2.7-std-i386-linux.tar.gz

        cd $SRC_DIR
        tar zxvf lsws-4.2.7-std-i386-linux.tar.gz
        cd lsws-4.2.7
        rm -f LICENSE
        
        expect -c "
        spawn ./install.sh
        expect \"license?\" { send \"Yes\r\" }
        expect \"Destination\" { send \"\r\" }
        expect \"User name\" { send \"$webuser\r\" }
        expect \"Password:\" { send \"$webpass\r\" }
        expect \"Retype password:\" { send \"$webpass\r\" }
        expect \"Email addresses\" { send \"$webemail\r\" }
        expect \"User\" { send \"www\r\" }
        expect \"Group\" { send \"www\r\" }
        expect \"HTTP port\" { send \"$port\r\" }
        expect \"Admin HTTP port\" { send \"\r\" }
        expect \"Setup up PHP\" { send \"Y\r\" }
        expect \"Suffix for PHP\" { send \"\r\" }
        expect \"Add-on module\" { send \"N\r\" }
        expect \"server restarts\" { send \"Y\r\" }
        expect \"right now\" { send \"Y\r\" }
        "

        sed -i 's/<vhRoot>\$SERVER_ROOT\/DEFAULT\/<\/vhRoot>/<vhRoot>\/home\/wwwroot\/default\/<\/vhRoot>/g' /usr/local/lsws/conf/httpd_config.xml
        sed -i 's/<configFile>\$VH_ROOT\/conf\/vhconf\.xml<\/configFile>/<configFile>\$SERVER_ROOT\/conf\/default\.xml<\/configFile>/g' /usr/local/lsws/conf/httpd_config.xml

        cp $PWD_DIR/conf/vhconf.xml /usr/local/lsws/conf/default.xml
        rm -rf /usr/local/lsws/DEFAULT/
        mkdir -p /home/wwwlogs/litespeed

        service lsws restart
    fi

    #install nginx
    if [ "$nginx_install" == "y" ]; then
        cp /usr/local/lsws/conf/httpd_config.xml /usr/local/lsws/conf/httpd_config.xml.bak
        proxy_ip="<useIpInProxyHeader>1<\/useIpInProxyHeader>"
        lend="<autoUpdateInterval>"
        sed -i 's/'$lend'/'$proxy_ip'\n&/' /usr/local/lsws/conf/httpd_config.xml
        service lsws stop
        killall -9 litespeed
        killall -9 lshttpd

        [ ! -s $SRC_DIR/pcre-8.34.tar.gz ] && wget -c $GET_URI/pcre/pcre-8.34.tar.gz -O $SRC_DIR/pcre-8.34.tar.gz

        [ "$jemalloc_install" == "y" ] && COMMAND="--with-ld-opt='-ljemalloc'"

        if [ "$nginx_select" == 2 ]; then
            [ ! -s $SRC_DIR/tengine-2.0.1.tar.gz ] && wget -c $GET_URI/tengine/tengine-2.0.1.tar.gz -O $SRC_DIR/tengine-2.0.1.tar.gz

            cd $SRC_DIR
            tar zxvf tengine-2.0.1.tar.gz
            cd tengine-2.0.1

            sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc

            ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_concat_module=shared $COMMAND
        else
            [ ! -s $SRC_DIR/nginx-1.4.6.tar.gz ] && wget -c $GET_URI/nginx/nginx-1.4.6.tar.gz -O $SRC_DIR/nginx-1.4.6.tar.gz

            cd $SRC_DIR
            tar zxvf nginx-1.4.6.tar.gz
            cd nginx-1.4.6

            sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc

            ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module $COMMAND
        fi

        make -j $cpu_num && make install

        mkdir -p /home/wwwlogs/nginx /usr/local/nginx/conf/vhosts
        rm -f /usr/local/nginx/conf/nginx.conf

        cat >/usr/local/nginx/conf/nginx.conf<<EOF
user                www www;
worker_processes    1;
error_log           /usr/local/nginx/logs/nginx_error.log  crit;
pid             /usr/local/nginx/nginx.pid;

worker_rlimit_nofile 51200;

events {
        use epoll;
        worker_connections 51200;
}

http {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml image/jpeg image/png image/gif;
        gzip_vary on;
        gzip_proxied        expired no-cache no-store private auth;
        gzip_disable        "MSIE [1-6]\.";

        #limit_zone  crawler  \$binary_remote_addr  10m;

        #log format
        log_format  access  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
             '\$status \$body_bytes_sent "\$http_referer" '
             '"\$http_user_agent" \$http_x_forwarded_for';
             
             include vhosts/*.conf;
}
EOF

        cat >/usr/local/nginx/conf/proxy.conf<<EOF
proxy_connect_timeout 300s;
proxy_send_timeout   900;
proxy_read_timeout   900;
proxy_buffer_size    32k;
proxy_buffers     4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect     off;
proxy_hide_header  Vary;
proxy_set_header   Accept-Encoding '';
proxy_set_header   Host   \$host;
proxy_set_header   Referer \$http_referer;
proxy_set_header   Cookie \$http_cookie;
proxy_set_header   X-Real-IP  \$remote_addr;
proxy_set_header   X-Forwarded-For \$proxy_add_x_forwarded_for;
EOF

        cat >/usr/local/nginx/conf/vhosts/default.conf<<EOF
log_format  default  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
             '\$status \$body_bytes_sent "\$http_referer" '
             '"\$http_user_agent" \$http_x_forwarded_for';
server {
    listen          $IP:80;
    server_name     shuang.ca;
    index           index.html index.htm index.php;
    root            /home/wwwroot/default;

    error_log       /home/wwwlogs/nginx/default_error.log;
    access_log      /home/wwwlogs/nginx/default_access.log;

    location / {
        try_files \$uri @litespeed;
    }

    location @litespeed {
        internal;
        proxy_pass  http://127.0.0.1:8088;
        include     proxy.conf;
    }

    location ~ .*\.(php|php5)?$ {
        proxy_pass  http://127.0.0.1:8088;
        include     proxy.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires     30d;
    }

    location ~ .*\.(js|css)?$ {
        expires     12h;
    }
}
EOF

        bit=$(getconf LONG_BIT)
        if [ "$bit" == "64" ]; then
            ln -s /usr/local/lib/libpcre.so.1 /lib64
        else
            ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
        fi

        if [ "$nginx_select" == 2 ]; then
            sed -i "s/worker_processes    1;/worker_processes    auto;\nworker_cpu_affinity auto;\ndso{\n\tload ngx_http_concat_module.so;\n}/g" /usr/local/nginx/conf/nginx.conf
        else
            [ "$cpu_num" == 2 ] && sed -i "s/worker_processes    1;/worker_processes    2;\nworker_cpu_affinity 10 01;/g" /usr/local/nginx/conf/nginx.conf
            [ "$cpu_num" == 4 ] && sed -i "s/worker_processes    1;/worker_processes    4;\nworker_cpu_affinity 1000 0100 0010 0001;/g" /usr/local/nginx/conf/nginx.conf
            [ "$cpu_num" == 6 ] && sed -i "s/worker_processes    1;/worker_processes    6;\nworker_cpu_affinity 100000 010000 001000 000100 000010 000001;/g" /usr/local/nginx/conf/nginx.conf
            [ "$cpu_num" == 8 ] && sed -i "s/worker_processes    1;/worker_processes    8;\nworker_cpu_affinity 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001;/g" /usr/local/nginx/conf/nginx.conf
        fi

        cp $PWD_DIR/conf/nginx /etc/init.d/nginx
        chmod +x /etc/init.d/nginx
        update-rc.d nginx defaults

        service lsws restart
        service nginx start
    fi

    #build php
    [ ! -s /usr/local/lsws/phpbuild ] && mkdir -p /usr/local/lsws/phpbuild

    if [ "$php_select" == 3 ]; then
        [ ! -s $SRC_DIR/php-5.5.9.tar.gz ] && wget -c $GET_URI/php/php-5.5.9.tar.gz -O $SRC_DIR/php-5.5.9.tar.gz

        cd $SRC_DIR
        tar zxvf php-5.5.9.tar.gz
        mv php-5.5.9 /usr/local/lsws/phpbuild
        cd /usr/local/lsws/phpbuild/php-5.5.9

        touch ac*
        rm -rf autom4te.*

        bit=$(getconf LONG_BIT)
        if [ "$bit" == "64" ]; then
            ln -s /usr/local/mysql/lib /usr/local/mysql/lib64
            ./configure '--disable-fileinfo' '--prefix=/usr/local/lsws/lsphp5' '--with-libdir=lib64' '--enable-opcache' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' '--with-iconv' '--with-freetype-dir=/usr/lib' '--with-jpeg-dir=/usr/lib' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-exif' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--with-mcrypt' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-ftp' '--with-gettext' '--enable-sysvshm' '--enable-magic-quotes' '--with-curlwrappers' '--with-ldap' '--with-ldap-sasl' '--enable-zip' '--enable-soap' '--disable-debug' '--with-litespeed'
        else
            ./configure '--disable-fileinfo' '--prefix=/usr/local/lsws/lsphp5' '--enable-opcache' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' '--with-iconv' '--with-freetype-dir=/usr/lib' '--with-jpeg-dir=/usr/lib' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-exif' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--with-mcrypt' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-ftp' '--with-gettext' '--enable-sysvshm' '--enable-magic-quotes' '--with-curlwrappers' '--with-ldap' '--with-ldap-sasl' '--enable-zip' '--enable-soap' '--disable-debug' '--with-litespeed'
        fi

        PLF=`uname -p`
        if [ "x$PLF" = "xx86_64" ] ; then
            DLSCH=`grep 'sys_lib_dlsearch_path_spec="/lib /usr/lib ' libtool`
            if [ "x$DLSCH" != "x" ] ; then
                cp libtool libtool.orig
                sed -e 's/sys_lib_dlsearch_path_spec=\"\/lib \/usr\/lib /sys_lib_dlsearch_path_spec=\"\/lib64 \/usr\/lib64 /' libtool.orig > libtool
            fi
        fi
        make clean
        make -j $cpu_num &&  make -k install

        cp /usr/local/lsws/phpbuild/php-5.5.9/php.ini-production /usr/local/lsws/lsphp5/lib/php.ini

        cd /usr/local/lsws/fcgi-bin

        [ -e "lsphp-5.5.9" ] && mv lsphp-5.5.9 lsphp-5.5.9.bak

        cp /usr/local/lsws/phpbuild/php-5.5.9/sapi/litespeed/php lsphp-5.5.9
        ln -sf lsphp-5.5.9 lsphp5
        ln -sf lsphp-5.5.9 lsphp55
        chmod a+rx lsphp-5.5.9
        chown -R lsadm:lsadm /usr/local/lsws/phpbuild/php-5.5.9

        sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/display_errors = On/display_errors = Off/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/lsws/lsphp5/lib/php.ini

        cat >>/usr/local/lsws/lsphp5/lib/php.ini<<EOF
[opcache]
zend_extension="/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/opcache.so"
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.save_comments=0
opcache.fast_shutdown=1
EOF
    elif [ "$php_select" == 2 ]; then
        [ ! -s $SRC_DIR/php-5.4.25.tar.gz ] && wget -c $GET_URI/php/php-5.4.25.tar.gz -O $SRC_DIR/php-5.4.25.tar.gz

        cd $SRC_DIR
        tar zxvf php-5.4.25.tar.gz
        mv php-5.4.25 /usr/local/lsws/phpbuild
        cd /usr/local/lsws/phpbuild/php-5.4.25

        touch ac*
        rm -rf autom4te.*

        bit=$(getconf LONG_BIT)
        if [ "$bit" == "64" ]; then
            ln -s /usr/local/mysql/lib /usr/local/mysql/lib64
            ./configure '--disable-fileinfo' '--prefix=/usr/local/lsws/lsphp5' '--with-libdir=lib64' '--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-zlib' '--with-gd' '--enable-shmop' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-magic-quotes' '--enable-mbstring' '--with-iconv' '--with-curl' '--with-curlwrappers' '--with-mcrypt' '--with-mhash' '--with-openssl' '--with-freetype-dir=/usr/lib' '--with-jpeg-dir=/usr/lib' '--with-png-dir' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-mbregex' '--enable-gd-native-ttf' '--enable-pcntl' '--with-ldap' '--with-ldap-sasl' '--with-xmlrpc' '--enable-zip' '--enable-inline-optimization' '--enable-soap' '--enable-ftp' '--disable-debug' '--with-gettext' '--enable-bcmath' '--with-litespeed'
        else
            ./configure '--disable-fileinfo' '--prefix=/usr/local/lsws/lsphp5' '--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-zlib' '--with-gd' '--enable-shmop' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-magic-quotes' '--enable-mbstring' '--with-iconv' '--enable-inline-optimization' '--with-curl' '--with-curlwrappers' '--with-mcrypt' '--with-mhash' '--with-openssl' '--with-freetype-dir=/usr/lib' '--with-jpeg-dir=/usr/lib' '--with-png-dir' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-mbregex' '--enable-gd-native-ttf' '--enable-pcntl' '--with-ldap' '--with-ldap-sasl' '--with-xmlrpc' '--enable-zip' '--enable-soap' '--enable-ftp' '--disable-debug' '--with-gettext' '--with-litespeed'
        fi

        PLF=`uname -p`
        if [ "x$PLF" = "xx86_64" ] ; then
            DLSCH=`grep 'sys_lib_dlsearch_path_spec="/lib /usr/lib ' libtool`
            if [ "x$DLSCH" != "x" ] ; then
                cp libtool libtool.orig
                sed -e 's/sys_lib_dlsearch_path_spec=\"\/lib \/usr\/lib /sys_lib_dlsearch_path_spec=\"\/lib64 \/usr\/lib64 /' libtool.orig > libtool
            fi
        fi
        make clean
        make -j $cpu_num &&  make -k install

        cp /usr/local/lsws/phpbuild/php-5.4.25/php.ini-production /usr/local/lsws/lsphp5/lib/php.ini

        cd /usr/local/lsws/fcgi-bin

        [ -e "lsphp-5.4.25" ] && mv lsphp-5.4.25 lsphp-5.4.25.bak

        cp /usr/local/lsws/phpbuild/php-5.4.25/sapi/litespeed/php lsphp-5.4.25
        ln -sf lsphp-5.4.25 lsphp5
        ln -sf lsphp-5.4.25 lsphp55
        chmod a+rx lsphp-5.4.25
        chown -R lsadm:lsadm /usr/local/lsws/phpbuild/php-5.4.25

        sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/display_errors = On/display_errors = Off/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/lsws/lsphp5/lib/php.ini
    else
        [ ! -s $SRC_DIR/php-5.3.28.tar.gz ] && wget -c $GET_URI/php/php-5.3.28.tar.gz -O $SRC_DIR/php-5.3.28.tar.gz

        [ ! -s $SRC_DIR/php-litespeed-6.6.tgz ] && wget -c $GET_URI/php-litespeed/php-litespeed-6.6.tgz -O $SRC_DIR/php-litespeed-6.6.tgz

        cd $SRC_DIR
        tar zxvf php-litespeed-6.6.tgz
        tar zxvf php-5.3.28.tar.gz
        cd php-5.3.28
        mv $SRC_DIR/litespeed $SRC_DIR/php-5.3.28/sapi/litespeed/
        cd $SRC_DIR
        mv php-5.3.28 /usr/local/lsws/phpbuild
        cd /usr/local/lsws/phpbuild/php-5.3.28

        touch ac*
        rm -rf autom4te.*
        yum -y install autoconf213
        export PHP_AUTOCONF=/usr/bin/autoconf-2.13
        ./buildconf --force

        bit=$(getconf LONG_BIT)
        if [ "$bit" == "64" ]; then
            ln -s /usr/local/mysql/lib /usr/local/mysql/lib64
            ./configure '--disable-fileinfo' '--prefix=/usr/local/lsws/lsphp5' '--with-libdir=lib64' '--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-zlib' '--with-gd' '--enable-shmop' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-magic-quotes' '--enable-mbstring' '--with-iconv' '--enable-inline-optimization' '--with-curl' '--with-curlwrappers' '--with-mcrypt' '--with-mhash' '--with-openssl' '--with-freetype-dir=/usr/lib' '--with-jpeg-dir=/usr/lib' '--with-png-dir' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-mbregex' '--enable-gd-native-ttf' '--enable-pcntl' '--with-ldap' '--with-ldap-sasl' '--with-xmlrpc' '--enable-zip' '--enable-soap' '--enable-ftp' '--disable-debug' '--with-gettext' '--enable-bcmath' '--with-litespeed'
        else
            ./configure '--disable-fileinfo' '--prefix=/usr/local/lsws/lsphp5' '--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' '--with-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-zlib' '--with-gd' '--enable-shmop' '--enable-exif' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-magic-quotes' '--enable-mbstring' '--with-iconv' '--with-curl' '--with-curlwrappers' '--with-mcrypt' '--with-mhash' '--with-openssl' '--with-freetype-dir=/usr/lib' '--with-jpeg-dir=/usr/lib' '--with-png-dir' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-mbregex' '--enable-gd-native-ttf' '--enable-pcntl' '--with-ldap' '--with-ldap-sasl' '--with-xmlrpc' '--enable-zip' '--enable-inline-optimization' '--enable-soap' '--disable-ipv6' '--enable-ftp' '--disable-debug' '--with-gettext' '--with-litespeed'
        fi

        make clean
        echo `date`
        make -j $cpu_num && make -k install

        cp /usr/local/lsws/phpbuild/php-5.3.28/php.ini-production /usr/local/lsws/lsphp5/lib/php.ini

        cd /usr/local/lsws/fcgi-bin

        [ -e "lsphp-5.3.28" ] && mv lsphp-5.3.28 lsphp-5.3.28.bak

        cp /usr/local/lsws/phpbuild/php-5.3.28/sapi/litespeed/php lsphp-5.3.28
        ln -sf lsphp-5.3.28 lsphp5
        ln -sf lsphp-5.3.28 lsphp55
        chmod a+rx lsphp-5.3.28
        chown -R lsadm:lsadm /usr/local/lsws/phpbuild/php-5.3.28

        sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/display_errors = On/display_errors = Off/g' /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/lsws/lsphp5/lib/php.ini
    fi

    service lsws restart

    #redis install
    if [ "$redis_install" == "y" ]; then
        [ ! -s $SRC_DIR/redis-2.2.4.tgz ] && wget -c $GET_URI/redis/lib/redis-2.2.4.tgz -O $SRC_DIR/redis-2.2.4.tgz

        cd $SRC_DIR
        tar zxvf redis-2.2.4.tgz
        cd redis-2.2.4
        /usr/local/lsws/lsphp5/bin/phpize
        ./configure --with-php-config=/usr/local/lsws/lsphp5/bin/php-config
        make -j $cpu_num && make install

        [ ! -s $SRC_DIR/redis-2.8.6.tar.gz ] && wget -c $GET_URI/redis/redis-2.8.6.tar.gz -O $SRC_DIR/redis-2.8.6.tar.gz

        cd $SRC_DIR
        tar zxvf redis-2.8.6.tar.gz
        cd redis-2.8.6

        if [ `getconf LONG_BIT` == 32 ]; then
            sed -i '1i\CFLAGS= -march=i686' src/Makefile
            sed -i 's@^OPT=.*@OPT=-O2 -march=i686@' src/.make-settings
        fi

        make -j $cpu_num

        if [ -f src/redis-server ]; then
            mkdir -p /usr/local/redis/{bin,etc,var}
            cp src/{redis-benchmark,redis-check-aof,redis-check-dump,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin/
            cp redis.conf /usr/local/redis/etc/
            ln -s /usr/local/redis/bin/* /usr/local/bin/
            sed -i 's@pidfile.*@pidfile /var/run/redis.pid@' /usr/local/redis/etc/redis.conf
            sed -i "s@logfile.*@logfile /usr/local/redis/var/redis.log@" /usr/local/redis/etc/redis.conf
            sed -i "s@^dir.*@dir /usr/local/redis/var@" /usr/local/redis/etc/redis.conf
            sed -i 's@daemonize no@daemonize yes@' /usr/local/redis/etc/redis.conf

            cp $PWD_DIR/conf/redis /etc/init.d/redis
            chmod +x /etc/init.d/redis
            update-rc.d redis defaults

            service redis start
        fi

        [ -z "`cat /usr/local/lsws/lsphp5/lib/php.ini | grep '^extension_dir'`" ] && sed -i "s@extension_dir = \"ext\"@extension_dir = \"ext\"\nextension_dir = \"/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`\"@" /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's@^extension_dir\(.*\)@extension_dir\1\nextension = "redis.so"@' /usr/local/lsws/lsphp5/lib/php.ini
    fi

    #memcache install
    if [ "$memcache_install" == "y" ]; then
        useradd -M -s /sbin/nologin memcached

        [ ! -s $SRC_DIR/memcached-1.4.17.tar.gz ] && wget -c $GET_URI/memcached/memcached-1.4.17.tar.gz -O $SRC_DIR/memcached-1.4.17.tar.gz

        cd $SRC_DIR
        tar zxvf memcached-1.4.17.tar.gz
        cd memcached-1.4.17
        ./configure --prefix=/usr/local/memcached
        make -j $cpu_num && make install

        ln -s /usr/local/memcached/bin/memcached /usr/bin/memcached
        cp $PWD_DIR/conf/memcached /etc/init.d/memcached
        chmod +x /etc/init.d/memcached
        update-rc.d memcached defaults

        service memcached start

        [ ! -s $SRC_DIR/memcache-2.2.7.tgz ] && wget -c $GET_URI/memcache/memcache-2.2.7.tgz -O $SRC_DIR/memcache-2.2.7.tgz

        cd $SRC_DIR
        tar zxvf memcache-2.2.7.tgz
        cd memcache-2.2.7
        /usr/local/lsws/lsphp5/bin/phpize
        ./configure --with-php-config=/usr/local/lsws/lsphp5/bin/php-config
        make -j $cpu_num && make install

        [ -z "`cat /usr/local/lsws/lsphp5/lib/php.ini | grep '^extension_dir'`" ] && sed -i "s@extension_dir = \"ext\"@extension_dir = \"ext\"\nextension_dir = \"/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`\"@" /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's@^extension_dir\(.*\)@extension_dir\1\nextension = "memcache.so"@' /usr/local/lsws/lsphp5/lib/php.ini

        [ ! -s $SRC_DIR/libmemcached-1.0.18.tar.gz ] && wget -c $GET_URI/libmemcached/libmemcached-1.0.18.tar.gz -O $SRC_DIR/libmemcached-1.0.18.tar.gz

        cd $SRC_DIR
        tar zxvf libmemcached-1.0.18.tar.gz
        cd libmemcached-1.0.18
        ./configure --with-memcached=/usr/local/memcached
        make -j $cpu_num && make install

        [ ! -s $SRC_DIR/memcached/lib/memcached-2.1.0.tgz ] && wget -c $GET_URI/memcached/lib/memcached-2.1.0.tgz -O $SRC_DIR/memcached-2.1.0.tgz

        cd $SRC_DIR
        tar zxvf memcached-2.1.0.tgz
        cd memcached-2.1.0
        /usr/local/lsws/lsphp5/bin/phpize
        ./configure --with-php-config=/usr/local/lsws/lsphp5/bin/php-config
        make -j $cpu_num && make install

        [ -z "`cat /usr/local/lsws/lsphp5/lib/php.ini | grep '^extension_dir'`" ] && sed -i "s@extension_dir = \"ext\"@extension_dir = \"ext\"\nextension_dir = \"/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`\"@" /usr/local/lsws/lsphp5/lib/php.ini
        sed -i 's@^extension_dir\(.*\)@extension_dir\1\nextension = "memcached.so"@' /usr/local/lsws/lsphp5/lib/php.ini
    fi

    #php cache
    if [ "$cache_install" == "y" ]; then
        if [ "$cache_select" == 3 ]; then
            [ ! -s $SRC_DIR/zendopcache-7.0.3.tgz ] && wget -c $GET_URI/zendopcache/zendopcache-7.0.3.tgz -O $SRC_DIR/zendopcache-7.0.3.tgz

            cd $SRC_DIR
            tar zxvf zendopcache-7.0.3.tgz
            cd zendopcache-7.0.3
            /usr/local/lsws/lsphp5/bin/phpize
            ./configure --with-php-config=/usr/local/lsws/lsphp5/bin/php-config
            make -j $cpu_num && make install

            if [ -f "/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/opcache.so" ];then
        cat >>/usr/local/lsws/lsphp5/lib/php.ini <<EOF
[opcache]
zend_extension="/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/opcache.so"
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.save_comments=0
opcache.fast_shutdown=1
EOF
            fi
        elif [ "$cache_select" == 2 ]; then
            [ ! -s $SRC_DIR/xcache-3.1.0.tar.gz ] && wget -c $GET_URI/xcache/xcache-3.1.0.tar.gz -O $SRC_DIR/xcache-3.1.0.tar.gz

            cd $SRC_DIR
            tar zxvf xcache-3.1.0.tar.gz
            cd xcache-3.1.0
            /usr/local/lsws/lsphp5/bin/phpize
            ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/lsws/lsphp5/bin/php-config
            make -j $cpu_num && make install

            cp -R htdocs /home/wwwroot/default/xcache
            chown -R www:www /home/wwwroot/default/xcache
            touch /tmp/xcache
            chown www:www /tmp/xcache

            cat >>/usr/local/lsws/lsphp5/lib/php.ini<<EOF
[xcache-common]
extension_dir = "/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/"
extension = "xcache.so"
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "21232f297a57a5a743894a0e4a801fc3"

[xcache]
xcache.cacher = On
xcache.size  = 20M
xcache.count = `cat /proc/cpuinfo | grep -c processor` 
xcache.slots = 8K
xcache.ttl = 3600
xcache.gc_interval = 300
xcache.var_size = 4M
xcache.var_count = 1
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.test = Off
xcache.readonly_protection = On
xcache.mmap_path = "/tmp/xcache"
xcache.coredump_directory = ""
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off

[xcache.coverager]
xcache.coverager = On
xcache.coveragedump_directory = ""
EOF
        else
            [ ! -s $SRC_DIR/eaccelerator-eaccelerator-42067ac.tar.gz ] && wget -c $GET_URI/eaccelerator/eaccelerator-eaccelerator-42067ac.tar.gz -O $SRC_DIR/eaccelerator-eaccelerator-42067ac.tar.gz

            cd $SRC_DIR
            tar zxvf eaccelerator-eaccelerator-42067ac.tar.gz
            cd eaccelerator-eaccelerator-42067ac
            /usr/local/lsws/lsphp5/bin/phpize
            ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/lsws/lsphp5/bin/php-config
            make -j $cpu_num && make install

            mkdir -p /tmp/eaccelerator

            cat >>/usr/local/lsws/lsphp5/lib/php.ini<<EOF
[eaccelerator]
extension_dir = "/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/"
extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
EOF
        fi
    fi

    #zend
    if [ "$zend_install" == "y" ]; then
        bit=$(getconf LONG_BIT)
        if [ "$bit" == "64" ]; then
            if [ "$php_select" == 2 ]; then
                [ ! -s $SRC_DIR/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz ] && wget -c $GET_URI/zend/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz -O $SRC_DIR/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz

                cd $SRC_DIR
                tar zxvf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz
                cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64/php-5.4.x/ZendGuardLoader.so /usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/
            else
                [ ! -s $SRC_DIR/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz ] && wget -c $GET_URI/zend/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz -O $SRC_DIR/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz

                cd $SRC_DIR
                tar zxvf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
                cp ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/
            fi
        else
            if [ "$php_select" == 2 ]; then
                [ ! -s $SRC_DIR/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz ] && wget -c $GET_URI/zend/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz -O $SRC_DIR/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz

                cd $SRC_DIR
                tar zxvf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
                cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386/php-5.4.x/ZendGuardLoader.so /usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/
            else
                [ ! -s $SRC_DIR/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz ] && wget -c $GET_URI/zend/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz -O $SRC_DIR/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz

                cd $SRC_DIR
                tar zxvf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
                cp ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so /usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/
            fi
        fi

        cat >> /usr/local/lsws/lsphp5/lib/php.ini<<EOF
[Zend Guard Loader]
zend_extension="/usr/local/lsws/lsphp5/lib/php/extensions/`ls /usr/local/lsws/lsphp5/lib/php/extensions/`/ZendGuardLoader.so"
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
EOF
    fi

    #pureftpd install
    if [ "$pureftpd_install" == "y" ]; then
        [ ! -s $SRC_DIR/pure-ftpd-1.0.36.tar.gz ] && wget -c $GET_URI/pureftpd/pure-ftpd-1.0.36.tar.gz -O $SRC_DIR/pure-ftpd-1.0.36.tar.gz

        cd $SRC_DIR
        cp /usr/local/mysql/lib/libmysqlclient* /usr/lib
        tar zxvf pure-ftpd-1.0.36.tar.gz
        cd pure-ftpd-1.0.36
        ./configure --prefix=/usr/local/pureftpd CFLAGS=-02 --with-mysql=/usr/local/mysql --with-quotas --with-cookie --with-virtualhosts --with-virtualroot --with-diraliases --with-sysquotas --with-ratios --with-altlog --with-paranoidmsg --with-shadow --with-welcomemsg --with-throttling --with-uploadscript --with-language=simplified-chinese
        make -j $cpu_num && make install

        cp configuration-file/pure-config.pl /usr/local/pureftpd/sbin/
        chmod 755 /usr/local/pureftpd/sbin/pure-config.pl
        cat >/usr/local/pureftpd/pureftpd-mysql.conf<<EOF
MYSQLSocket     /tmp/mysql.sock
MYSQLServer     localhost
MYSQLPort       3306
MYSQLUser       ftp
MYSQLPassword   $mysqlftppwd
MYSQLDatabase   ftpusers
MYSQLCrypt  md5
MYSQLGetPW      SELECT Password FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
MYSQLGetUID     SELECT Uid FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
MYSQLGetGID     SELECT Gid FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
MYSQLGetDir     SELECT Dir FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
MySQLGetBandwidthUL SELECT ULBandwidth FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
MySQLGetBandwidthDL SELECT DLBandwidth FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
EOF

        cat >/usr/local/pureftpd/pure-ftpd.conf<<EOF
ChrootEveryone              yes
BrokenClientsCompatibility  no
MaxClientsNumber            50
Daemonize                   yes
MaxClientsPerIP             5
VerboseLog                  no
DisplayDotFiles             yes
AnonymousOnly               no
NoAnonymous                 yes
SyslogFacility              ftp
DontResolve                 yes
MaxIdleTime                 15
MySQLConfigFile             /usr/local/pureftpd/pureftpd-mysql.conf
UnixAuthentication          yes
LimitRecursion              2000 8
AnonymousCanCreateDirs      no
MaxLoad                     4
PassivePortRange            20000 30000
AntiWarez                   yes
Umask                       133:022
MinUID                      100
AllowUserFXP                no
AllowAnonymousFXP           no
ProhibitDotFilesWrite       no
ProhibitDotFilesRead        no
AutoRename                  no
AnonymousCantUpload         no
CreateHomeDir               no
MaxDiskUsage                99
CustomerProof               yes
AllowOverwrite              on
AllowStoreRestart           on
EOF

        cat >$SRC_DIR/script.mysql<<EOF
INSERT INTO mysql.user (Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv) VALUES('localhost','ftp',PASSWORD('$mysqlftppwd'),'Y','Y','Y','Y','N','N','N','N','N','N','N','N','N','N');
GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'ftp'@'localhost' IDENTIFIED BY  '$mysqlftppwd';
FLUSH PRIVILEGES;
drop database if exists ftpusers;
CREATE DATABASE ftpusers;
USE ftpusers;
CREATE TABLE admin (
    Username varchar(35) NOT NULL default '',
    Password char(32) binary NOT NULL default '',
    PRIMARY KEY  (Username)
) ENGINE=MyISAM;
INSERT INTO admin VALUES ('Administrator',MD5('$ftpmanagerpwd'));
CREATE TABLE users (
    User varchar(16) NOT NULL default '',
    Password varchar(32) binary NOT NULL default '',
    Uid int(11) NOT NULL default '14',
    Gid int(11) NOT NULL default '5',
    Dir varchar(128) NOT NULL default '',
    QuotaFiles int(10) NOT NULL default '0',
    QuotaSize int(10) NOT NULL default '0',
    ULBandwidth int(10) NOT NULL default '0',
    DLBandwidth int(10) NOT NULL default '0',
    Ipaddress varchar(15) NOT NULL default '*',
    Comment tinytext,
    Status enum('0','1') NOT NULL default '1',
    ULRatio smallint(5) NOT NULL default '1',
    DLRatio smallint(5) NOT NULL default '1',
    PRIMARY KEY  (User),
    UNIQUE KEY User (User)
) ENGINE=MyISAM;
EOF

        /usr/local/mysql/bin/mysql -u root -p$dbpass -h localhost < $SRC_DIR/script.mysql

        cp $SRC_DIR/conf/pureftpd /etc/init.d/pureftpd
        chmod +x /etc/init.d/pureftpd
        update-rc.d pureftpd defaults

        [ ! -s $SRC_DIR/User_manager_for-PureFTPd_v2.1_CN.zip ] && wget -c $GET_URI/pureftpd/User_manager_for-PureFTPd_v2.1_CN.zip -O $SRC_DIR/User_manager_for-PureFTPd_v2.1_CN.zip

        cd $SRC_DIR
        unzip User_manager_for-PureFTPd_v2.1_CN.zip
        mv ftp /home/wwwroot/default/
        chmod 777 -R /home/wwwroot/default/ftp/
        chown -R www:www /home/wwwroot/default/ftp/

        sed -i 's/English/Chinese/g' /home/wwwroot/default/ftp/config.php
        sed -i 's/tmppasswd/'$mysqlftppwd'/g' /home/wwwroot/default/ftp/config.php
        sed -i 's/myipaddress.com/localhost/g' /home/wwwroot/default/ftp/config.php

        iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
        iptables -I INPUT 6 -p tcp -m state --state NEW -m tcp --dport 20000:30000 -j ACCEPT
        service iptables save
        service iptables restart

        service pureftpd start
    fi

    #config
    mkdir -p /home/wwwroot/default/phpmyadmin/

    cp $PWD_DIR/conf/p.php /home/wwwroot/default/p.php
    cp $PWD_DIR/conf/llnmp.jpg /home/wwwroot/default/llnmp.jpg
    cp $PWD_DIR/conf/index.html /home/wwwroot/default/index.html
    sed -i "s/{ip}/$IP/g" /home/wwwroot/default/index.html

    [ ! -s $SRC_DIR/phpMyAdmin-4.1.5-all-languages.tar.gz ] && wget -c $GET_URI/phpmyadmin/phpMyAdmin-4.1.5-all-languages.tar.gz -O $SRC_DIR/phpMyAdmin-4.1.5-all-languages.tar.gz

    cd $SRC_DIR
    tar zxvf phpMyAdmin-4.1.5-all-languages.tar.gz
    cp -rf phpMyAdmin-4.1.5-all-languages/* /home/wwwroot/default/phpmyadmin/
    chown -R www:www /home/wwwroot/
}